var Promise = require('bluebird');
var cheerio = require('cheerio');
var _ = require('lodash');

var mongoBase = require('../common/mongo-base');
var requestBase = require('../common/request-base');

module.exports = {
  getNewsData: getNewsData,
  getArticleData: getArticleData
};


function getNewsData(urlIdx) {

  var aLinks = [];

  var promiseArr = [];

  for(var i = 1; i <= urlIdx; i++) {
    (function(urlIdx) {
      promiseArr.push(requestBase.requestData('http://roll.sky.news.sina.com.cn/index/index_' + urlIdx + '.shtml'));
    })(i);
  }

  return Promise.all(promiseArr)
    .then(function(results) {
      _.forEach(results, function(data) {
        var $ = cheerio.load(data);
        $('#d_list').find('.c_tit a').each(function() {
          var link = $(this).attr('href');
          if(/^http:\/\/sky\.news\.sina\.com\.cn\/\d+\-\d+\-\d+\/\d+\.html$/.test(link)){
            aLinks.push(link);
          }
        });
      });
    })

    .then(function() {
      var promiseArr = [];
      _.forEach(aLinks, function(link) {
        promiseArr.push(getArticleData(link));
      });
      return Promise.all(promiseArr)
    })

    .catch(function(err) {
      console.log(err);
    });

}

function getArticleData(url) {
  var $ = {};
  var articleData = {};

  return requestBase.requestBufferData(url, 'GBK')
    .then(function(data) {
      $ = cheerio.load(data, {decodeEntities: false});
    })

    .then(function() {
      var $JArticleWrap = $('#J_Article_Wrap');

      var $artibodyTitle = $JArticleWrap.find('#artibodyTitle');
      var $mediaName = $JArticleWrap.find('#media_name').find('[data-sudaclick]');
      var $artibody = $JArticleWrap.find('#artibody');
      var $img = $artibody.find('div.img_wrapper img');
      var $artKeywords = $artibody.find('p.art_keywords');

      var keywords = [];
      $artKeywords.find('a').each(function(idx, dom) {
        keywords.push($(dom).html());
      });

      $artKeywords.remove();
      $artibody.find('div.show_author').remove();
      $artibody.find('div.content_line').remove();
      var ellipsis = '';
      $artibody.find('p').each(function(idx, dom) {
        if(idx < 3) {
          ellipsis +=  $(dom).html() + '<br/>'
        } else {
          return ;
        }
      });
      var pubDate = $JArticleWrap.find('#pub_date').html();
      var date = new Date(Date.parse(pubDate.replace('年','-').replace('月','-').replace('日','')));

      articleData = {
        originalLink: url,
        artibodyTitle: $artibodyTitle.html(),
        mediaName: $mediaName.html(),
        medialink: $mediaName.attr('href'),
        artibody: $artibody.html(),
        photo: $img.eq(0).attr('src'),
        ellipsis: ellipsis,
        pubDate: pubDate,
        date: date,
        keywords: keywords
      };
      articleData.hisPhoto = articleData.photo ? true : false;
    })
  
    .then(function() {
      var query = {'artibodyTitle': articleData.artibodyTitle};
      var fields = {_id:1};
      return mongoBase.findOneData('news-article', query, fields);
    })

    .then(function(data) {
      if(data && data._id) {
        return mongoBase.updateData('news-article', {_id: data._id}, articleData);
      } else {
        return mongoBase.saveData('news-article', articleData);
      }
    })

    .catch(function(err) {
      console.log(err);
    });

}